<template>
  <view class="followers-container">
    <!-- 导航栏 -->
    <nav-bar></nav-bar>
    <!-- 统计卡片 -->
    <view class="stats-card">
      <view class="stat-item">
        <text class="value">{{ totalFollowers }}</text>
        <text class="label">关注总数</text>
      </view>
      <view class="stat-item">
        <text class="value">{{ newFollowers }}</text>
        <text class="label">本月新增</text>
      </view>
      <view class="stat-item">
        <text class="value">{{ activeFollowers }}</text>
        <text class="label">活跃客户</text>
      </view>
    </view>

    <!-- 客户列表 -->
    <view class="followers-list">
      <view class="list-header">
        <text class="title">关注列表</text>
        <text class="count">共{{ totalFollowers }}位客户</text>
      </view>
      
      <scroll-view scroll-y class="list-content" @scrolltolower="loadMore">
        <view v-if="followers.length > 0">
          <view class="follower-item" v-for="(item, index) in followers" :key="index">
            <image class="avatar" :src="item.avatar" mode="aspectFill"></image>
            <view class="info">
              <view class="name-row">
                <text class="name">{{ item.nickname }}</text>
                <text class="follow-time">{{ item.followTime }}</text>
              </view>
              <view class="stats-row">
                <text class="stat-text">下单{{ item.orderCount }}次</text>
                <text class="stat-text">好评{{ item.goodReviews }}次</text>
              </view>
            </view>
          </view>
        </view>
        
        <!-- 空状态 -->
        <view v-else class="empty-state">
          <image src="/static/icons/empty-followers.png" mode="aspectFit"></image>
          <text>暂无客户关注</text>
        </view>

        <!-- 加载更多 -->
        <view class="loading-more" v-if="followers.length > 0">
          <text v-if="hasMore">加载中...</text>
          <text v-else>没有更多了</text>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
import NavBar from '@/components/nav-bar/nav-bar.vue'
export default {
	components: {
		NavBar
	},
  data() {
    return {
      totalFollowers: 0,
      newFollowers: 0,
      activeFollowers: 0,
      followers: [],
      page: 1,
      pageSize: 20,
      hasMore: true
    }
  },

  onLoad() {
    this.loadStats()
    this.loadFollowers()
  },

  methods: {
    goBack() {
      uni.navigateBack()
    },

    async loadStats() {
      try {
        const result = await uni.request({
          url: `${uni.$config.baseUrl}/member/followers/stats`,
          method: 'GET',
          header: {
            'Authorization': uni.getStorageSync('token')
          }
        })

        if (result.data.code === 200) {
          const { total, newCount, active } = result.data.data
          this.totalFollowers = total
          this.newFollowers = newCount
          this.activeFollowers = active
        }
      } catch (err) {
        uni.showToast({
          title: '加载失败',
          icon: 'none'
        })
      }
    },

    async loadFollowers() {
      if (!this.hasMore) return

      uni.showLoading({
        title: '加载中...'
      })

      try {
        const result = await uni.request({
          url: `${uni.$config.baseUrl}/member/followers/list`,
          method: 'GET',
          data: {
            page: this.page,
            pageSize: this.pageSize
          },
          header: {
            'Authorization': uni.getStorageSync('token')
          }
        })

        if (result.data.code === 200) {
          const { list, hasMore } = result.data.data
          this.followers = this.page === 1 ? list : [...this.followers, ...list]
          this.hasMore = hasMore
          this.page++
        }
      } catch (err) {
        uni.showToast({
          title: '加载失败',
          icon: 'none'
        })
      } finally {
        uni.hideLoading()
      }
    },

    loadMore() {
      if (this.hasMore) {
        this.loadFollowers()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.followers-container {
  min-height: 100vh;
  background: #F5F5F5;

  .custom-nav-bar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 88rpx;
    background: #FFFFFF;
    display: flex;
    align-items: center;
    padding: 0 30rpx;
    z-index: 100;
    
    .left {
      width: 88rpx;
      display: flex;
      align-items: center;
      
      .iconfont {
        font-size: 36rpx;
        color: #333333;
      }
    }
    
    .center {
      flex: 1;
      text-align: center;
      font-size: 32rpx;
      color: #333333;
      font-weight: 500;
    }
    
    .right {
      width: 88rpx;
    }
  }

  .stats-card {
    margin: 20rpx 30rpx 30rpx;
    background: linear-gradient(135deg, #FF6B6B 0%, #FF4B4B 100%);
    border-radius: 16rpx;
    padding: 30rpx;
    display: flex;
    justify-content: space-around;

    .stat-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      color: #FFFFFF;

      .value {
        font-size: 40rpx;
        font-weight: bold;
        margin-bottom: 8rpx;
      }

      .label {
        font-size: 24rpx;
      }
    }
  }

  .followers-list {
    margin: 0 30rpx;
    background: #FFFFFF;
    border-radius: 16rpx;
    
    .list-header {
      padding: 30rpx;
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-bottom: 1rpx solid #F5F5F5;

      .title {
        font-size: 32rpx;
        color: #333333;
        font-weight: 500;
      }

      .count {
        font-size: 28rpx;
        color: #999999;
      }
    }

    .list-content {
      height: calc(100vh - 400rpx);

      .follower-item {
        padding: 30rpx;
        display: flex;
        align-items: center;
        border-bottom: 1rpx solid #F5F5F5;

        .avatar {
          width: 100rpx;
          height: 100rpx;
          border-radius: 50%;
          margin-right: 20rpx;
        }

        .info {
          flex: 1;

          .name-row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 12rpx;

            .name {
              font-size: 32rpx;
              color: #333333;
              font-weight: 500;
            }

            .follow-time {
              font-size: 24rpx;
              color: #999999;
            }
          }

          .stats-row {
            display: flex;
            gap: 20rpx;

            .stat-text {
              font-size: 24rpx;
              color: #666666;
            }
          }
        }
      }
    }

    .empty-state {
      padding: 100rpx 0;
      display: flex;
      flex-direction: column;
      align-items: center;
      color: #999999;

      image {
        width: 200rpx;
        height: 200rpx;
        margin-bottom: 20rpx;
      }
    }

    .loading-more {
      padding: 30rpx;
      text-align: center;
      color: #999999;
      font-size: 24rpx;
    }
  }
}
</style> 